/**
 * Created on 13 avr. 08 by Sebastian Audet
 */
package gui2Dengine;

import grid.Grid;
import grid.GridOutOfBounds;
import grid.Location;

import java.awt.Shape;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author Sebastian Audet
 * 
 */
public class TriangleDisplayMap extends DisplayMap {

    HashMap<Location, EquilateralTriangle> pointmap;

    TriangleDisplayMap(Grid grid, double length, double width) {
	super(grid, length, width);
	shapes = new ArrayList<Shape>();
	shapemap = new HashMap<Location, Shape>();
	pointmap = new HashMap<Location, EquilateralTriangle>();
    }

    @Override
    public void generateShapeData(int xoffset, int yoffset, int xunits,
	    int yunits) {
	double bl = EquilateralTriangle.getBisectorLength(length);
	Point2D.Double p = new Point2D.Double(0, bl);
	boolean switcher; //First row or second row - First = UP, Second = Down
	boolean switcher2; //Up or Down
	if ((yoffset % 2) == 0) {
	    switcher = true;
	} else {
	    switcher = false;
	}
	switcher2 = switcher;
	for (int i = 0; i < yunits; i++) {
	    for (int j = 0; j < xunits; j++) {
		Point2D.Double point = null;
		if (switcher2) {
			point = new Point2D.Double(p.x + (j * length/2), p.y
				+ (i * bl));
		    } else {
			point = new Point2D.Double(p.x
				+ (j * length/2), (i * bl));
		    }
		EquilateralTriangle tri = new EquilateralTriangle(point,
			length, switcher2);
		Shape shape = tri.getTriangle();
		try {
		    pointmap.put(grid.getNode(
			    new Location(j + yoffset, i + xoffset))
			    .getLocation(), tri);
		    shapemap.put(grid.getNode(
			    new Location(j + yoffset, i + xoffset))
			    .getLocation(), shape);
		} catch (GridOutOfBounds e) {
		    e.printStackTrace();
		}
		shapes.add(shape);

		switcher2 = !switcher2;
	    }
	    switcher = !switcher;
	    switcher2 = switcher;
	} // End TriangleGrid Code
    }

}
